// pages/mineModule/record/record.js
import wxCharts from '../../../utils/wxcharts'
var lineChart = null;
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    formData: {},
    rules: [{
      name: 'todayWeight',
      rules: {required: true, message: '体重是必填项'}
    },{
      name: 'todayHeight',
      rules: {required: true, message: '体重是必填项'}
    },{
      name: 'standard',
      rules: {required: false, message: ''}
    }]
  },
  touchHandler: function (e) {
    console.log(lineChart.getCurrentDataIndex(e));
    lineChart.showToolTip(e, {
        // background: '#7cb5ec',
        format: function (item, category) {
            return category + ' ' + item.name + ':' + item.data 
        }
    });
},    
createSimulationData: function () {
    var categories = [];
    var data = [];
    for (var i = 0; i < 7; i++) {
        categories.push('2016-' + (i + 1));
        data.push(i);
    }
    // data[4] = null;
    return {
        categories: categories,
        data: data
    }
},
// updateData: function () {
//     var simulationData = this.createSimulationData();
//     var series = [{
//         name: '成交量1',
//         data: simulationData.data,
//         format: function (val, name) {
//             return val.toFixed(2) + '万';
//         }
//     }];
//     lineChart.updateData({
//         categories: simulationData.categories,
//         series: series
//     });
// },
  formInputChange(e) {
    const {field} = e.currentTarget.dataset
    this.setData({
        [`formData.${field}`]: e.detail.value
    })
  },
  submitForm () {
    let message = '';
    if (app.globalData.userInfo.gender == 1) {
      message = ((this.data.formData.todayHeight - 80) * 0.7) < this.data.formData.todayWeight ? "较重，望减肥":(
        ((this.data.formData.todayHeight - 80) * 0.7) == this.data.formData.todayWeight? "标准，望保持！": "较轻，望多吃！");
    } else {
      message = ((this.data.formData.todayHeight - 70) * 0.6) < this.data.formData.todayWeight ? "较重，望减肥":(
        ((this.data.formData.todayHeight - 70) * 0.6) == this.data.formData.todayWeight? "标准，望保持！": "较轻，望多吃！"
        );
    }
    console.log(message);
    
    this.setData({
      [`formData.standard`] : message
    });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var windowWidth = 320;
        var simulationData = this.createSimulationData();
        lineChart = new wxCharts({
            canvasId: 'lineCanvas',
            type: 'line',
            categories: simulationData.categories,
            animation: true,
            // background: '#f5f5f5',
            series: [{
                name: '身高',
                data: simulationData.data,
                format: function (val, name) {
                    return val.toFixed(2) + 'cm';
                }
            }, {
                name: '体重',
                data: [1,2, 3,4, 5,6,7],
                format: function (val, name) {
                    return val.toFixed(2) + 'kg';
                }
            }],
            xAxis: {
              title: '日期',
              disableGrid: true
            },
            yAxis: {
                title: '数字',
                format: function (val) {
                    return val.toFixed(2);
                },
                min: 0
            },
            width: windowWidth,
            height: 200,
            dataLabel: false,
            dataPointShape: true,
            extra: {
                lineStyle: 'curve'
            }
        });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})